import { observable, action, toJS } from 'mobx';
import { Toast, Tools, Bubble } from 'weaver-mobile';
import { ListViewPage, SearchAdvanced, SearchPage } from 'weaver-mobile-page';

import Api from '../../apis/attachment';
const rootPath = '/attachment';
const { getLabel } = Tools;

const initState = {
  showCheck: false,
  islight: false,
  showPopover: false,
  file: { filename: '' },
  searchKey: 'filename',
  searchParams: {},
  template: { default: [{ key: 'col', configs: [{ key: 'col_row', configs: [{ key: 'name' }] }] }] },
  routes: {
    searchAd: `${rootPath}/searchAdvanced`,
    searchPage: `${rootPath}/search`,
    filePreview: `${rootPath}/filePreview`,
    writeNew: `${rootPath}/new`,
    mailView: `${rootPath}/view`,
  },
  newParams: {
    fileid: [],
		title: getLabel('30912','写信'),
		titleTextFunc: () => { return getLabel('81300','写信') },
  },
  mailid: '',
  localeStorageKey: 'searchHistory__email_attachment',
  sysConfig: {},
  prohibitDownload:"0",
};

export default class AttachmentStore {

  rootPath = rootPath;
  template = initState.template;
  routes = initState.routes;
  localeStorageKey = initState.localeStorageKey;

  @observable file = initState.file;
  @observable searchKey = initState.searchKey;
  @observable searchParams = initState.searchParams;
  @observable islight = initState.islight;
  @observable showCheck = initState.showCheck;
  @observable showPopover = initState.showPopover;
  @observable params = initState.params;
  @observable newParams = initState.newParams;
  @observable mailid = initState.mailid;
  @observable sysConfig = initState.sysConfig;
  @observable listViewStore = new ListViewPage.Store();
  @observable searchAdvancedStore = new SearchAdvanced.Store({ route: rootPath, id: 'searchAdvanced' });
  @observable searchPageStore = new SearchPage.Store({ route: rootPath, id: 'search', template: initState.template });
  @observable prohibitDownload = initState.prohibitDownload;
  

  @action
  getData = (params) => {
    try {
      Api.attachmentList(params).then(action((res) => {
        const { sessionkey,prohibitDownload } = res;
        this.listViewStore.getData({ dataKey: sessionkey }).setState({ isTable: false });
        this.prohibitDownload = prohibitDownload;
      }));
    } catch (e) {
      Toast.info(e, 1);
    }
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)

  @action
  configUrl = () => {
    Api.configUrl().then(res => {
      if (res.status === '1') {
        this.sysConfig = res.sysConfig;
      } else {
        Bubble.error(res.msg);
      }
    })
  }

   // 更新list数据
   updateListData = (ids, params, isDelete = false) => {
    const { data, updateDataSource, setState } = this.listViewStore;
    let curData = [];
    if (isDelete) {
      curData = toJS(data).filter(v => ids.split(',').indexOf(v.mailid) < 0);
    } else {
      curData = toJS(data).map(v => {
        if (ids.split(',').indexOf(v.mailid) > -1) {
          return {
            ...v,
            ...params,
          }
        }
        return v;
      });
    }
    Promise.all([setState({ data: curData})]).then(action(() => {
      setState({ selectedRowKeys: [] });
      updateDataSource();
    }));
  }
}
